package com.lingying.caiwudailikehudaun.controller.login;


import com.alibaba.fastjson.JSONObject;
import com.lingying.caiwudailikehudaun.domain.UserBean;
import com.lingying.caiwudailikehudaun.domain.verification.VerificationCode;
import com.lingying.caiwudailikehudaun.domain.waiBuRenYuan.WaiBuRenYuanBean;
import com.lingying.caiwudailikehudaun.mapper.keHuUserLogin.KuHuUserLoginMapper;
import com.lingying.caiwudailikehudaun.verificationUtils.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/login")
public class KeHuUserLogin {

    @Resource
    HttpServletRequest httpServletRequest;
    @Resource
    KuHuUserLoginMapper keHuUserLoginMapper;

    /**
     * 使用验证码登录，比对验证码是否通过
     * 桑满  20210427
     * @param keHuUsePhone
     * @param YZM
     * @return
     */
    @PostMapping("yanZhengMaLogin")
    public Object yanZhengMaLogin(String keHuUsePhone,String YZM){
        String key="bind:caiwudailikehuduan:"+keHuUsePhone;
        JSONObject object=new JSONObject();
        //拿到之前存入map中的key
        VerificationCode code=VerificationCodeUtil.map.get(key);
        //如果为空则表示档当前手机好未发送验证码
        if(code==null){
            object.put("code",500);
            object.put("msg","验证码不正确");
            return object;
        }
        //判断当前验证码手否正确
        if(!YZM.equals(code.getCaiwudailikehuduan())){
            object.put("code",500);
            object.put("msg","验证码不正确");
            return object;
        }
        //销毁map中的key
        VerificationCodeUtil.map.remove(key);
        //查询当前手机号是否有注册如果有装进fenPeiKeHuUser对象
//        FenPeiKeHuUser fenPeiKeHuUser = keHuUserLoginMapper.selectUserByPhone(keHuUsePhone);
        UserBean userBean = keHuUserLoginMapper.selectUserByPhone(keHuUsePhone);
        if(userBean==null){
            object.put("code",500);
            object.put("msg","登陆失败！请确认是否注册。");
            return object;
        }else{
            object.put("code",0);
            object.put("msg","登录成功");
            return object;
        }
    }


    /**
     * 获取验证码的接口
     * 桑满  20210427
     * @param keHuUsePhone
     * @return
     */
    @PostMapping("/yanZhengMa")
    public Object yanZhengMa(String keHuUsePhone){
        JSONObject object=new JSONObject();
        //判断当前的手机号格式是否正确
        if(!CheckUtil.isPhone(keHuUsePhone)){
            object.put("code",500);
            object.put("msg","手机号格式不正确");
            return object;
        }
        try {
            //获取4位随机验证码
            String yanZhengMa= RandomUtils.getNumberCharInt(4);
            String key="bind:caiwudailikehuduan:"+keHuUsePhone;
            //将key存入VerificationCode对象中
            VerificationCode datas = VerificationCodeUtil.map.get(key);
            if(datas==null){
            }else{
                //获取到发送验证码的时间并于当前时间相比较，判断是否小于4分钟
                Date time=datas.getTime();
                Date date=new Date();
                int i = DateUtil.secondBetweenByDateStr(time, date);
                if(i<-240){
                    object.put("code",500);
                    object.put("msg","发送验证码过于频繁");
                    return object;
                }
            }
            TXyunMessageUtil.mains(keHuUsePhone,yanZhengMa,418634);
            //临时存储验证码和时间
            VerificationCode verificationCode=new VerificationCode();
            //把验证码设置进临时存储对象中
            verificationCode.setCaiwudailikehuduan(yanZhengMa);
            VerificationCodeUtil.map.put(key,verificationCode);

            object.put("code",200);
            object.put("msg","发送成功");
            return object;
        } catch (Exception e) {
            e.printStackTrace();
            object.put("code",500);
            object.put("msg","发送失败,请刷新重试");
            return object;
        }
    }


    /**
     * 根据账号密码去外部人员表进行数据比对，做登录操作
     * 桑满  20210602
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/keHuUserLogin")
    public Object keHuUserLogin(String username,String password){

        JSONObject object=new JSONObject();
        List<WaiBuRenYuanBean> waiBuRenYuanList= keHuUserLoginMapper.keHuUserLogin(username);
//        System.err.println(waiBuRenYuanList);

        //判断当前集合对象是否为空
        if(waiBuRenYuanList!=null){
            //遍历对象结果集
            for (WaiBuRenYuanBean waiBuRenYuanBean : waiBuRenYuanList) {
                //拿到外部人员id
                Integer userId = waiBuRenYuanBean.getWaiBuRenYuanId();
                //拿到结果集的用户名密码
                String phone = waiBuRenYuanBean.getPhone();
                String passWord = waiBuRenYuanBean.getPassword();
                //拿到当前登录人在外部人员表中重要信息
                Integer jiGouId = waiBuRenYuanBean.getJiGouId();
                String userName = waiBuRenYuanBean.getWaiBuRenYuanName();
                Integer jueSeId = waiBuRenYuanBean.getJueSe();

                //封装session
                httpServletRequest.getSession().setAttribute("userId",userId);
                httpServletRequest.getSession().setAttribute("jiGouId",jiGouId);
                httpServletRequest.getSession().setAttribute("phone",phone);
                httpServletRequest.getSession().setAttribute("userName",userName);
                httpServletRequest.getSession().setAttribute("phone",phone);
                httpServletRequest.getSession().setAttribute("jueSeId",jueSeId);
                //登陆成功之后清空之前缓存的做账申报id避免出现假数据
                httpServletRequest.getSession().removeAttribute("zuoZhangShenBaoId");

                //和输入框值得数据做比对
                if(passWord.equals(password)){
                    if(phone.equals(username)){
                        object.put("code",200);
                        object.put("msg","登录成功！");
                        object.put("data",httpServletRequest.getSession().getAttribute("userId"));
                        return object;
                    }else{
                        object.put("code",500);
                        object.put("msg","账号异常");
                        return object;
                    }
                }else{
                    continue;
                }
            }
        }else{
            object.put("code",500);
            object.put("msg","账号异常");
            return object;
        }
        return object;
    }

    /**
     * 根据账号密码去外部人员表进行数据比对，做登录操作
     * 桑满  20210602
     * @return
     */
    @PostMapping("/getUInfo")
    public Object getUInfo(){
        JSONObject object=new JSONObject();
        String userName= (String) httpServletRequest.getSession().getAttribute("userName");
        if(userName!=null){
            object.put("code",200);
            object.put("msg","查询成功");
            object.put("data",userName);
        }else{
            object.put("code",500);
            object.put("msg","查询失败");
        }
        return object;
    }
}












